<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddCommonUserFields extends Migration {

	/**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{
	    // 增加
		Schema::table('user', function(Blueprint $table) {
			$table->string('realname', 60)->nullable()->defaults('');//真实姓名
			$table->string('credit',30)->nullable()->defaults('');//身份证
			$table->string('province',100)->nullable()->defaults('');
			$table->string('city',100)->nullable()->defaults('');
			$table->string('district',100)->nullable()->defaults('');
			$table->string('address', 120)->nullable()->default('');//详细地址
		});
		
		// 数据移植
		// - 把原来的customer和lawyer里面的用户通用数据放到user表格
		DB::update('
		    UPDATE user u 
		    JOIN lawyer l ON u.id = l.user_id
		    SET u.realname = l.realname,
		    u.credit = l.credit,
		    u.province = l.province,
		    u.city = l.city,
		    u.district = l.district,
		    u.address = l.address
		');
		
		DB::update('
		    UPDATE user u 
		    JOIN customer c ON u.id = c.user_id
		    SET u.realname = c.realname,
		    u.credit = c.credit,
		    u.province = c.province,
		    u.city = c.city,
		    u.district = c.district,
		    u.address = c.address
		');
	}

	/**
	 * Reverse the migrations.
	 *
	 * @return void
	 */
	public function down()
	{
		Schema::table('user', function(Blueprint $table) {
			$table->dropColumn('realname');//真实姓名
			$table->dropColumn('credit');//身份证
			$table->dropColumn('province');
			$table->dropColumn('city');
			$table->dropColumn('district');
			$table->dropColumn('address');//详细地址
		});
	}

}
